#!/usr/bin/env python3
# -*- coding:utf-8 -*-
# @Time: 2019/6/11  11:18
# @Author: 余浪人
# @email: yulangren520@gmail.com
from apps.lib.public import get_date, web_page_auto, webApp_check
from apps.models.other_model import AdCooperation_Model, Infringement_Model, Feedback_Model, Contribute_Model
from apps.webapp import web_bp
from flask import render_template, g, request
from apps.models.system_model import System_Model, Blogroll_Model, Ad_Model, db
from apps.models.column_model import Column_model
from apps.models.document_model import Document_Model
from apps.lib.public import check_obj
import random


# 首页
@web_bp.route('/', endpoint='tl_index', methods=['GET'])
@webApp_check
def index():
    g.time = get_date()
    g.sys = System_Model.query.filter().first()  # 系统信息
    g.column = Column_model.query.order_by(db.asc(Column_model.column_order)).filter(
        Column_model.status == 1).all()  # 分类信息
    g.document = Document_Model.query.filter(
        Document_Model.status == 1, Document_Model.update_time < get_date()).all()  # 文档信息
    g.todayNum = Document_Model.query.filter(
        Document_Model.status == 1, Document_Model.update_time == get_date().strftime('%Y-%m-%d')).count()  # 今日更新
    g.random_document = random.sample(
        Document_Model.query.filter(Document_Model.status == 1, Document_Model.update_time < get_date()).all(),
        12 if len(g.document) >= 12 else len(g.document))  # 随机推荐
    g.links = Blogroll_Model.query.filter(Blogroll_Model.status == 1).all()  # 友情链接
    g.other = {
        "ad": Ad_Model.query.filter(Ad_Model.status == 1, Ad_Model.ad_end_date >= get_date()).all(),  # 广告位
        "adCooperation": AdCooperation_Model.query.filter().first(),  # 广告合作
        "contribute": Contribute_Model.query.filter().first(),  # 投稿说明
        "feedback": Feedback_Model.query.filter().first(),  # 留言反馈
        "infringement": Infringement_Model.query.filter().first(),  # 侵权反馈
    }  # 综合
    return render_template('web_templates/index.html')


# 列表页
@web_bp.route('/list_article/<uuid>.tl_cms', endpoint='list_article', methods=['GET'])
@webApp_check
def list_article(uuid):
    col_obj = Column_model.query.filter_by(uuid=uuid, status=1).first()
    if not check_obj(col_obj):
        g.sys = System_Model.query.filter().first()  # 系统信息
        g.column = Column_model.query.order_by(db.asc(Column_model.column_order)).filter(
            Column_model.status == 1).all()  # 分类信息
        g.document = Document_Model.query.filter(
            Document_Model.status == 1, Document_Model.update_time < get_date()).all()  # 文档信息
        g.random_document = random.sample(
            Document_Model.query.filter(Document_Model.status == 1, Document_Model.update_time < get_date()).all(),
            12 if len(g.document) >= 12 else len(g.document))  # 随机推荐
        g.col_first = col_obj  # 当前分类信息
        g.col_doc, g.page = web_page_auto(Document_Model.query.order_by(db.desc(Document_Model.is_list_stick)).order_by(db.desc(Document_Model.id)).filter(
            Document_Model.superior == col_obj.id, Document_Model.status == 1))  # 当前分类文档
        g.other = {
            "ad": Ad_Model.query.filter(Ad_Model.status == 1, Ad_Model.ad_end_date >= get_date()).all(),  # 广告位
            "adCooperation": AdCooperation_Model.query.filter().first(),  # 广告合作
            "contribute": Contribute_Model.query.filter().first(),  # 投稿说明
            "feedback": Feedback_Model.query.filter().first(),  # 留言反馈
            "infringement": Infringement_Model.query.filter().first(),  # 侵权反馈
        }  # 综合
        return render_template('web_templates/list_article.html')


# 详情页
@web_bp.route('/article_article/<uuid>.tl_cms', endpoint='article_article', methods=['GET'])
@webApp_check
def article_article(uuid):
    document_obj = Document_Model.query.filter_by(uuid=uuid, status=1).first()
    if not check_obj(document_obj):
        g.time = get_date()
        g.sys = System_Model.query.filter().first()  # 系统信息
        g.column = Column_model.query.order_by(db.asc(Column_model.column_order)).filter(
            Column_model.status == 1).all()  # 分类信息
        g.document = Document_Model.query.filter(
            Document_Model.status == 1, Document_Model.update_time < get_date()).all()  # 文档信息
        g.random_document = random.sample(
            Document_Model.query.filter(Document_Model.status == 1, Document_Model.update_time < get_date()).all(),
            12 if len(g.document) >= 12 else len(g.document))  # 随机推荐
        g.col_first = document_obj.pro.uuid  # 所属分类信息
        g.doc_first = document_obj  # 文档信息
        g.per = Document_Model.query.filter(
            Document_Model.status == 1, Document_Model.id < document_obj.id).first()  # 上一篇
        g.next = Document_Model.query.filter(
            Document_Model.status == 1, Document_Model.id > document_obj.id).first()  # 下一篇
        g.other = {
            "ad": Ad_Model.query.filter(Ad_Model.status == 1, Ad_Model.ad_end_date >= get_date()).all(),  # 广告位
            "adCooperation": AdCooperation_Model.query.filter().first(),  # 广告合作
            "contribute": Contribute_Model.query.filter().first(),  # 投稿说明
            "feedback": Feedback_Model.query.filter().first(),  # 留言反馈
            "infringement": Infringement_Model.query.filter().first(),  # 侵权反馈
        }  # 综合
        return render_template('web_templates/article_article.html')


@web_bp.route('/ad', endpoint='adCooperation', methods=['GET'])
@webApp_check
def adCooperation():
    g.sys = System_Model.query.filter().first()  # 系统信息
    g.column = Column_model.query.order_by(db.asc(Column_model.column_order)).filter(
        Column_model.status == 1).all()  # 分类信息
    g.document = Document_Model.query.filter(
        Document_Model.status == 1, Document_Model.update_time < get_date()).all()  # 文档信息
    g.random_document = random.sample(
        Document_Model.query.filter(Document_Model.status == 1, Document_Model.update_time < get_date()).all(),
        12 if len(g.document) >= 12 else len(g.document))  # 随机推荐
    g.other = {
        "ad": Ad_Model.query.filter(Ad_Model.status == 1, Ad_Model.ad_end_date >= get_date()).all(),  # 广告位
        "adCooperation": AdCooperation_Model.query.filter().first(),  # 广告合作
        "contribute": Contribute_Model.query.filter().first(),  # 投稿说明
        "feedback": Feedback_Model.query.filter().first(),  # 留言反馈
        "infringement": Infringement_Model.query.filter().first(),  # 侵权反馈
    }  # 综合
    return render_template('web_templates/ad.html')


@web_bp.route('/contribute', endpoint='contribute', methods=['GET'])
@webApp_check
def contribute():
    g.sys = System_Model.query.filter().first()  # 系统信息
    g.column = Column_model.query.order_by(db.asc(Column_model.column_order)).filter(
        Column_model.status == 1).all()  # 分类信息
    g.document = Document_Model.query.filter(
        Document_Model.status == 1, Document_Model.update_time < get_date()).all()  # 文档信息
    g.random_document = random.sample(
        Document_Model.query.filter(Document_Model.status == 1, Document_Model.update_time < get_date()).all(),
        12 if len(g.document) >= 12 else len(g.document))  # 随机推荐
    g.other = {
        "ad": Ad_Model.query.filter(Ad_Model.status == 1, Ad_Model.ad_end_date >= get_date()).all(),  # 广告位
        "adCooperation": AdCooperation_Model.query.filter().first(),  # 广告合作
        "contribute": Contribute_Model.query.filter().first(),  # 投稿说明
        "feedback": Feedback_Model.query.filter().first(),  # 留言反馈
        "infringement": Infringement_Model.query.filter().first(),  # 侵权反馈
    }  # 综合
    return render_template('web_templates/contribute.html')


@web_bp.route('/feedback', endpoint='feedback', methods=['GET'])
@webApp_check
def feedback():
    g.sys = System_Model.query.filter().first()  # 系统信息
    g.column = Column_model.query.order_by(db.asc(Column_model.column_order)).filter(
        Column_model.status == 1).all()  # 分类信息
    g.document = Document_Model.query.filter(
        Document_Model.status == 1, Document_Model.update_time < get_date()).all()  # 文档信息
    g.random_document = random.sample(
        Document_Model.query.filter(Document_Model.status == 1, Document_Model.update_time < get_date()).all(),
        12 if len(g.document) >= 12 else len(g.document))  # 随机推荐
    g.other = {
        "ad": Ad_Model.query.filter(Ad_Model.status == 1, Ad_Model.ad_end_date >= get_date()).all(),  # 广告位
        "adCooperation": AdCooperation_Model.query.filter().first(),  # 广告合作
        "contribute": Contribute_Model.query.filter().first(),  # 投稿说明
        "feedback": Feedback_Model.query.filter().first(),  # 留言反馈
        "infringement": Infringement_Model.query.filter().first(),  # 侵权反馈
    }  # 综合
    return render_template('web_templates/feedback.html')


@web_bp.route('/infr', endpoint='infringement', methods=['GET'])
@webApp_check
def infringement():
    g.sys = System_Model.query.filter().first()  # 系统信息
    g.column = Column_model.query.order_by(db.asc(Column_model.column_order)).filter(
        Column_model.status == 1).all()  # 分类信息
    g.document = Document_Model.query.filter(
        Document_Model.status == 1, Document_Model.update_time < get_date()).all()  # 文档信息
    g.random_document = random.sample(
        Document_Model.query.filter(Document_Model.status == 1, Document_Model.update_time < get_date()).all(),
        12 if len(g.document) >= 12 else len(g.document))  # 随机推荐
    g.other = {
        "ad": Ad_Model.query.filter(Ad_Model.status == 1, Ad_Model.ad_end_date >= get_date()).all(),  # 广告位
        "adCooperation": AdCooperation_Model.query.filter().first(),  # 广告合作
        "contribute": Contribute_Model.query.filter().first(),  # 投稿说明
        "feedback": Feedback_Model.query.filter().first(),  # 留言反馈
        "infringement": Infringement_Model.query.filter().first(),  # 侵权反馈
    }  # 综合
    return render_template('web_templates/infr.html')

# 搜索功能
@web_bp.route('/search', endpoint='search', methods=['GET'])
@webApp_check
def search():
    keywords = request.args.get('keywords')
    g.sys = System_Model.query.filter().first()  # 系统信息
    g.column = Column_model.query.order_by(db.asc(Column_model.column_order)).filter(
        Column_model.status == 1).all()  # 分类信息
    g.document = Document_Model.query.filter(
        Document_Model.status == 1, Document_Model.update_time < get_date()).all()  # 文档信息
    g.random_document = random.sample(
        Document_Model.query.filter(Document_Model.status == 1, Document_Model.update_time < get_date()).all(),
        12 if len(g.document) >= 12 else len(g.document))  # 随机推荐
    g.other = {
        "ad": Ad_Model.query.filter(Ad_Model.status == 1, Ad_Model.ad_end_date >= get_date()).all(),  # 广告位
        "adCooperation": AdCooperation_Model.query.filter().first(),  # 广告合作
        "contribute": Contribute_Model.query.filter().first(),  # 投稿说明
        "feedback": Feedback_Model.query.filter().first(),  # 留言反馈
        "infringement": Infringement_Model.query.filter().first(),  # 侵权反馈
    }  # 综合
    g.col_doc, g.page = web_page_auto(
        Document_Model.query.order_by(db.desc(Document_Model.id)).filter(Document_Model.status == 1, Document_Model.title.like(f'%{keywords}%')))
    return render_template('web_templates/search.html',keyword=keywords)


